package com.nc.mem.serial.graph.asm;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

import org.objectweb.asm.Type;

import com.nc.mem.serial.graph.cust.KeyOnlyPayload;
import com.nc.mem.serial.graph.cust.OnlyPayload;
import com.nc.mem.serial.graph.cust.ValOnlyPayload;


public class ReflexiveFieldInfo extends FieldInfo {

	Field field;

	public ReflexiveFieldInfo(final String owner, final Field field, final long slot, final int flags) {
		super(owner, field.getName(), Type.getType(field.getType()), slot, flags);
		this.field = field;
		if (field.isSynthetic()) {
			treatAsPrivate();
		}
	}

	@Override
	public String desc() {
		return Type.getDescriptor(field.getType());
	}

	@Override
	public boolean isCollection() {

		return Collection.class.isAssignableFrom(field.getType());
	}

	@Override
	public boolean isMap() {
		return Map.class.isAssignableFrom(field.getType());
	}

	@Override
	public boolean isOnlyPayloadCollection() {
		return Collection.class.isAssignableFrom(type()) && field.isAnnotationPresent(OnlyPayload.class);
	}

	@Override
	public boolean isOnlyPayloadMapKey() {
		return Map.class.isAssignableFrom(field.getType()) && field.isAnnotationPresent(KeyOnlyPayload.class);
	}

	@Override
	public boolean isOnlyPayloadMapVal() {
		return Map.class.isAssignableFrom(field.getType()) && field.isAnnotationPresent(ValOnlyPayload.class);
	}

	@Override
	public String name() {

		return field.getName();
	}

	@Override
	public String owner() {
		return owner;
	}

	@Override
	public Class<?> type() {

		return field.getType();
	}

	@Override
	public String typeName() {

		return field.getType().getName();
	}

}
